// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.core.skinnedmodel.runtime;

import java.util.List;
import zombie.core.math.PZMath;
import zombie.core.skinnedmodel.ModelManager;
import zombie.core.skinnedmodel.animation.AnimationClip;
import zombie.core.skinnedmodel.animation.Keyframe;
import zombie.scripting.ScriptParser;

public final class CopyFrames implements IRuntimeAnimationCommand {
    protected int m_frame;
    protected int m_FPS = 30;
    protected String m_source;
    protected int m_sourceFrame1;
    protected int m_sourceFrame2;
    protected int m_sourceFPS = 30;

    @Override
    public void parse(ScriptParser.Block block) {
        for (ScriptParser.Value value : block.values) {
            String string0 = value.getKey().trim();
            String string1 = value.getValue().trim();
            if ("source".equalsIgnoreCase(string0)) {
                this.m_source = string1;
            } else if ("frame".equalsIgnoreCase(string0)) {
                this.m_frame = PZMath.tryParseInt(string1, 1);
            } else if ("sourceFrame1".equalsIgnoreCase(string0)) {
                this.m_sourceFrame1 = PZMath.tryParseInt(string1, 1);
            } else if ("sourceFrame2".equalsIgnoreCase(string0)) {
                this.m_sourceFrame2 = PZMath.tryParseInt(string1, 1);
            }
        }
    }

    @Override
    public void exec(List<Keyframe> keyframes) {
        AnimationClip animationClip = ModelManager.instance.getAnimationClip(this.m_source);

        for (int int0 = 0; int0 < 60; int0++) {
            Keyframe[] keyframe0 = animationClip.getBoneFramesAt(int0);
            if (keyframe0.length != 0) {
                for (int int1 = this.m_sourceFrame1; int1 <= this.m_sourceFrame2; int1++) {
                    Keyframe keyframe1 = keyframe0[0];
                    Keyframe keyframe2 = new Keyframe();
                    keyframe2.Bone = keyframe1.Bone;
                    keyframe2.BoneName = keyframe1.BoneName;
                    keyframe2.Time = (float)(this.m_frame - 1 + (int1 - this.m_sourceFrame1)) / (float)this.m_FPS;
                    keyframe2.Position = KeyframeUtil.GetKeyFramePosition(
                        keyframe0, (float)(int1 - 1) / (float)this.m_sourceFPS, (double)animationClip.Duration
                    );
                    keyframe2.Rotation = KeyframeUtil.GetKeyFrameRotation(
                        keyframe0, (float)(int1 - 1) / (float)this.m_sourceFPS, (double)animationClip.Duration
                    );
                    keyframe2.Scale = keyframe1.Scale;
                    keyframes.add(keyframe2);
                }
            }
        }
    }
}
